// user.js - 用户信息模块
const state = {
  userInfo: {
    nickname: '登录/注册',
    avatar: 'http://47.92.70.20:39013/youxuegou/static/image/uplodImg/no_login.png'
  },
  userId: null,
  userToken: null,
  isLogin: false
};

const mutations = {
  // 更新用户信息
  updateUserInfo(state, userInfo) {
    if (userInfo) {
      state.userInfo = userInfo;
      state.userId = userInfo.id;
      state.isLogin = true;
    } else {
      state.userInfo = {
        nickname: '登录/注册',
        avatar: 'http://47.92.70.20:39013/youxuegou/static/image/uplodImg/no_login.png'
      };
      state.userId = null;
      state.isLogin = false;
    }
  },
  
  // 更新用户ID
  updateUserId(state, userId) {
    state.userId = userId;
  },
  
  // 更新用户Token
  updateUserToken(state, token) {
    state.userToken = token;
  },
  
  // 更新登录状态
  updateLoginStatus(state, status) {
    state.isLogin = status;
  },
  
  // 编辑用户信息（部分更新）
  editUserInfo(state, info) {
    state.userInfo = { ...state.userInfo, ...info };
  },
  
  // 清除用户信息（登出）
  clearUserInfo(state) {
    state.userInfo = {
      nickname: '登录/注册',
      avatar: 'http://47.92.70.20:39013/youxuegou/static/image/uplodImg/no_login.png'
    };
    state.userId = null;
    state.userToken = null;
    state.isLogin = false;
  }
};

const getters = {
  // 是否登录
  isLogin: state => state.isLogin,
  
  // 获取用户信息
  getUserInfo: state => state.userInfo,
  
  // 获取用户ID
  getUserId: state => state.userId,
  
  // 获取用户Token
  getUserToken: state => state.userToken
};

const actions = {
  // 登录成功后的操作
  loginSuccess({ commit }, userData) {
	  console.log(444,userData)
	  uni.setStorageSync('user_token', userData.user_token);
	  uni.setStorageSync('user_info', userData);
    commit('updateUserInfo', userData);
    commit('updateUserToken', userData.user_token);
   
  },
  
  // 登出操作
  logout({ commit }) {
    commit('clearUserInfo');
    uni.removeStorageSync('user_token');
    uni.removeStorageSync('user_info');
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
};